package net.ipinglun;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    RecyclerView mWaterfall;
    Context mContext;

    String[] data = {"AAA", "BBB", "CCC", "DDDD", "EEEE", "FFFFF", "GGGG", "HHHH", "III", "JJJJ", "KKKK", "1111", "2222", "3333", "4444", "5555", "6666"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = this;

        mWaterfall = (RecyclerView) findViewById(R.id.recyclerView);
        //mWaterfall.addItemDecoration(new SpacesItemDecoration(100));

        //View headerView = LayoutInflater.from(this).inflate(R.layout.waterfall_header, null);
        //mWaterfall.addHeaderView(headerView);

        StaggeredGridLayoutManager mgr = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);


        mWaterfall.setLayoutManager(mgr);
        //设置适配器
        mWaterfall.setAdapter(new WaterfallAdapter());
    }

    //继承自 RecyclerView.Adapter
    class WaterfallAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        public static final int TYPE_HEADER = 0;
        public static final int TYPE_NORMAL = 1;



        @Override
        public int getItemViewType(int position) {
            if (position == 0) {
                return TYPE_HEADER;
            } else {
                return TYPE_NORMAL;
            }
        }

        //RecyclerView显示的子View
        //该方法返回是ViewHolder，当有可复用View时，就不再调用
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if (TYPE_HEADER == viewType) {
                final View view = LayoutInflater.from(mContext).inflate(R.layout.waterfall_header, parent, false);
                StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
                layoutParams.setFullSpan(true);
                view.setLayoutParams(layoutParams);
                return new HeaderViewHolder(view);
            } else if (TYPE_NORMAL == viewType) {
                final View view = LayoutInflater.from(mContext).inflate(R.layout.waterfall_item, parent, false);
                //StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
                //layoutParams.setFullSpan(true);
                //view.setLayoutParams(layoutParams);
                return new ViewHolder(view);
            }

            return null;

            /*
            View view = getLayoutInflater().inflate(R.layout.waterfall_item, null);
            //StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            //layoutParams.setFullSpan(true);
            //view.setLayoutParams(layoutParams);

            return new ViewHolder(view);
            */
        }

        //将数据绑定到子View
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {



            //viewHolder.textView.setText(data[i]);

            //RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            //        RelativeLayout.LayoutParams.MATCH_PARENT,
            //        200 + (i * 10));
            //viewHolder.imageView.setLayoutParams(lp);

        }

        //RecyclerView显示数据条数
        @Override
        public int getItemCount() {
            return data.length;
        }


        //自定义的ViewHolder,减少findViewById调用次数
        class HeaderViewHolder extends RecyclerView.ViewHolder {

            TextView textView;
            ImageView imageView;

            public HeaderViewHolder(View itemView) {
                super(itemView);
                //textView = (TextView) itemView.findViewById(R.id.textView);
                //imageView = (ImageView) itemView.findViewById(R.id.imageView);
            }
        }

        //自定义的ViewHolder,减少findViewById调用次数
        class ViewHolder extends RecyclerView.ViewHolder {

            TextView textView;
            ImageView imageView;

            public ViewHolder(View itemView) {
                super(itemView);
                textView = (TextView) itemView.findViewById(R.id.textView);
                imageView = (ImageView) itemView.findViewById(R.id.imageView);
            }
        }
    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;

        public SpacesItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {


            System.out.println("outRect=" + state);
            // Add top margin only for the first item to avoid double space between items
            if(parent.getChildPosition(view) == 0) {
                outRect.top = space;
                outRect.left = -space;
                outRect.right = space;
                outRect.bottom = space;
            }
        }
    }


}